import { Component } from '@angular/core';
import { NavParams, NavController, ToastController } from 'ionic-angular';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

import { StorageService } from '../../providers/StorageService';
import { SettlementService } from '../../providers/SettlementService';

@Component({
  selector: 'page-settlement',
  templateUrl: 'settlement.html',
  providers: [SettlementService]
})
export class SettlementPage {
  settlementForm: FormGroup;
  user_info: any;
  user_id: number;
  room: any;
  room_id: number;
  room_name: string;
  build_address: string;
  settlement_id: number;
  check_in_id: number;
  check_in_date: any;
  check_out_date: any;

  constructor(
    navParams: NavParams,
    private navCtrl: NavController,
    private toastCtrl: ToastController,
    private formBuilder: FormBuilder,
    private storageService: StorageService,
    private settlementService: SettlementService
  ) {
    this.user_info = this.storageService.read('UserInfo');
    this.user_id = this.user_info.user_id;

    this.room = this.storageService.read('RoomInfo');
    this.room_id = this.room.room_id;
    this.room_name = this.room.room_name;
    this.build_address = this.room.build_address;

    this.check_in_id = navParams.get('check_in_id');

    this.settlementService.getDate({'room_id': this.room_id, 'check_in_id': this.check_in_id}).then(data => {
      this.check_in_date = data.check_in_date;
      this.check_out_date = data.check_out_date;
    });

    this.settlementForm = this.formBuilder.group({
      'settlement_month': ['', [Validators.required]],
      'settlement_electricity': ['', [Validators.required]],
      'settlement_water': ['', [Validators.required]],
      'settlement_additional_amount': [''],
      'settlement_remark': [''],
    });

    this.settlement_id = typeof navParams.get('settlement_id') === 'undefined' ? 0 : navParams.get('settlement_id');

    if (this.settlement_id) {
      this.settlementService.get({'user_id': this.user_id, 'room_id': this.room_id, 'settlement_id': this.settlement_id}).then(data => {
        this.settlementForm.controls['settlement_month'].disable();
        this.settlementForm.controls['settlement_electricity'].disable();
        this.settlementForm.controls['settlement_water'].disable();
        this.settlementForm.controls['settlement_additional_amount'].disable();

        this.settlementForm.controls['settlement_month'].setValue(data.settlement.settlement_month);
        this.settlementForm.controls['settlement_electricity'].setValue(data.settlement.settlement_electricity);
        this.settlementForm.controls['settlement_water'].setValue(data.settlement.settlement_water);
        this.settlementForm.controls['settlement_additional_amount'].setValue(data.settlement.settlement_additional_amount);
        this.settlementForm.controls['settlement_remark'].setValue(data.settlement.settlement_remark);
      });
    }
  }

  save(settlement, _event) {
    settlement.user_id = this.user_info.user_id;
    settlement.room_id = this.room_id;
    settlement.check_in_id = this.check_in_id;
    settlement.settlement_id = typeof this.settlement_id === 'undefined' ? 0 : this.settlement_id;
    let error_message = '保存失败';

    _event.preventDefault();
    this.settlementService.save(settlement).then(data => {
      if (data.status == 200)
      {
        this.navCtrl.pop();
        let toast = this.toastCtrl.create({
          message: '保存成功.',
          duration: 3000,
          position: 'top',
          showCloseButton: true,
          closeButtonText: '关闭'
        });
        toast.present();
      }
      else
      {
        error_message = data.error.message;
        let toast = this.toastCtrl.create({
          message: error_message,
          duration: 3000,
          position: 'top',
          showCloseButton: true,
          closeButtonText: '关闭'
        });
        toast.present();
      }
    });
  }
}
